package com.woniu.microownerserver.test;

import com.alibaba.fastjson.JSON;
import com.woniu.microownerserver.bean.BookInfo;
import com.woniu.microownerserver.bean.User;
import com.woniu.microownerserver.mapper.BookMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Mytest {
    @Resource
    private BookMapper bookMapper;

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
    @Test
    public void fT() throws IOException {
        System.out.println(client);
        //创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("uww");
        //执行索引请求
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
    @Test
    public void fT2() throws IOException {
        //创建索引请求
        GetIndexRequest request = new GetIndexRequest("uww");
        //执行索引请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    //添加文档操作
    @Test
    public void doc() throws IOException {
        User user = new User("廖健",21);
        //创建索引请求
        IndexRequest request = new IndexRequest("uww");
        //规则 put /uww/_doc/1
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        //将数据放入请求
        request.source(JSON.toJSONString(user), XContentType.JSON);
        //发送请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.status());

    }

    //获取文档操作
    @Test
    public void getdoc() throws IOException{
        //判断文档是否存在
        GetRequest request = new GetRequest("uww","1");
        System.out.println(client.exists(request,RequestOptions.DEFAULT));
        //拿文档
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String string = response.getSourceAsString();
        System.out.println(string);
        System.out.println(response);
    }

    //修改文档 ,版本有问题
    @Test
    public void updatedoc() throws IOException{
        UpdateRequest request = new UpdateRequest("uww","1");
        User user = new User();
        user.setName("汪广");
        request.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }

    //转移数据To es
    @Test
    public void getAllBookInfo() throws IOException {
        List<BookInfo> allBooks = bookMapper.getAllBooks();
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        for (BookInfo bookinfo : allBooks) {
            bulkRequest.add(new IndexRequest("books_info")
                    .source(JSON.toJSONString(bookinfo),XContentType.JSON)
            );
        }
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.hasFailures());
    }

    //批量操作
    @Test
    public void mutiladddoc() throws IOException{
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        ArrayList list = new ArrayList();
        list.add(new User("罗欢",25));
        list.add(new User("纯纯",23));
        list.add(new User("何杰",24));
        list.add(new User("刘青",29));
        for (Object o : list) {
            bulkRequest.add(new IndexRequest("jd_goodssqqq")
            .source(JSON.toJSONString(o),XContentType.JSON)
            );
        }
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.hasFailures());
    }

    //批量获取
    @Test
    public void getdocs() throws IOException{
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        searchRequest.source(sourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(response.getHits().getTotalHits());

    }
}
